package com.example.nioprc;


import com.example.nioprc.application.service.Impl.HelloServiceImpl;
import com.example.nioprc.infrastructure.client.NioRpcClient;
import com.example.nioprc.infrastructure.proxy.RpcProxyFactory;
import com.example.nioprc.infrastructure.serializer.JsonSerializer;
import com.example.nioprc.infrastructure.server.NioRpcServer;
import com.example.nioprc.interfaces.api.HelloService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class NioPrcApplication {

    public static void main(String[] args) {
        new Thread(NioPrcApplication::startServer).start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log.error("error,e:", e);
        }
        startClient();
    }
    
    /**
     * 启动RPC服务端
     */
    private static void startServer(){
        NioRpcServer server = new NioRpcServer(8888);
        server.registerService(HelloService.class, new HelloServiceImpl());
        server.start();
    }
    
    private static void startClient(){
        //创建RPC客户端
        NioRpcClient rpcClient = new NioRpcClient("127.0.0.1", 8888,new JsonSerializer());
        //创建代理工厂
        RpcProxyFactory rpcProxyFactory = new RpcProxyFactory(rpcClient);
        //创建服务代理
        HelloService helloService = rpcProxyFactory.createProxy(HelloService.class);
        String result = helloService.sayHello("World");
        log.info("客户端接收到：{}", result);
        
    }

}
